package com.ygqh.localsign.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ygqh.common.okhttp.DefaultOkHttp3Client;
import com.ygqh.common.okhttp.exception.OkHttp3Exception;
import com.ygqh.customs.domain.PayExInfo;
import com.ygqh.customs.request.DataUpRequest;
import com.ygqh.customs.response.PlatDataResponse;
import com.ygqh.localsign.socket.WebCallback;
import com.ygqh.localsign.socket.WebSocketClientHandle;
import com.ygqh.localsign.utils.LocalSignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 加签入口
 * <p>
 *
 * @author: 张弓
 * @date: 2018/12/27
 * @version: 1.0.0
 */
@Slf4j
@RequestMapping("/customs")
@RestController
public class SignController {

    @Autowired
    private WebSocketClientHandle webSocketClientHandle;

    @Value("${yiigoo.localDataUpUrl}")
    private String localDataUpUrl;

    /**
     * 加签调用方法
     * <p>
     */
    @PostMapping("/sign")
    public void sign(@RequestBody PayExInfo request) {

        log.info("入参-{}", request);

        String signStr = LocalSignUtil.buildLocalParamStr(request);
        System.out.println(signStr);
        webSocketClientHandle.send(request.getSessionID(), signStr, new WebCallback() {

            @Override
            public void message(String key, Object param) {
                log.info("key: {}, message: {}", key, param);
                String signValue;
                if (!StringUtils.isEmpty(param)) {
                    JSONObject jsonObject = JSON.parseObject(String.valueOf(param));
                    Integer id = jsonObject.getInteger("_id");
                    if (id != null && id > 0) {
                        JSONObject args = jsonObject.getJSONObject("_args");
                        JSONArray data = args.getJSONArray("Data");
                        signValue = data.getString(0);
                        DataUpRequest dataUpRequest = new DataUpRequest();
                        dataUpRequest.setSessionID(request.getSessionID());
                        dataUpRequest.setSignValue(signValue);
                        dataUpRequest.setServiceTime(request.getServiceTime());
                        dataUpRequest.setDataUpId(request.getDataUpId());
                        doLocalDataUp(dataUpRequest);
                    }
                }

            }

            @Override
            public void error(Object param) {
                log.error("localSign fail: {}", param);
            }
        });
    }


    private void doLocalDataUp(DataUpRequest dataUpRequest) {
        try {
            DefaultOkHttp3Client.httpPost(localDataUpUrl, JSON.toJSONString(dataUpRequest), DefaultOkHttp3Client.JSON_MEDIA_TYPE, new TypeReference<PlatDataResponse>() {
            });
        } catch (OkHttp3Exception e) {
            e.printStackTrace();
            log.error("doLocalDataUp fail: {}", e);
        }
    }

}
